package com.leboncoin.html;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.leboncoin.bean.ClassifiedBean;

public class LeboncoinHtmlSearchResult {

	static Logger logger = Logger.getLogger(LeboncoinHtmlSearchResult.class.getName());	
	List<Element> rootElements;
	
	public LeboncoinHtmlSearchResult(String htmlSource) {
		super();
		Document document = Jsoup.parse(htmlSource);
		//System.out.println(htmlSource);
		this.rootElements = new ArrayList<Element>();
		Elements root = document.select("div[class=list-lbc]");
		if (root != null && root.size()>0) {
			List<Element> elements = document.select("div[class=list-lbc]").get(0).children();
			for (Element rootElement:elements){
				if (rootElement.tagName().equalsIgnoreCase("a")) {
					this.rootElements.add(rootElement);
				}
			}
		}
	}

	public List<ClassifiedBean> getClassifiedList(Boolean daySearch){
		
		List<ClassifiedBean> classifiedList = new ArrayList<ClassifiedBean>();
		
		Elements elements = getFilteredTrList(daySearch); // liste des tr du jour

		for (Iterator<Element> iterator = elements.iterator(); iterator.hasNext();) {
			Element element = iterator.next();

			ClassifiedBean classifiedBean = new ClassifiedBean();
			

			if (populateTitle(element, classifiedBean)) {
				classifiedList.add(classifiedBean);
			}
		
		}		
		return classifiedList;
	}
	
	
	public Elements getFilteredTrList(Boolean daySearch){
		
		Elements result = new Elements();
		

		for (Element rootElement:rootElements){
			
			// Seulement les tr d'aujourd'hui
			Boolean keepThisAd = false;
			if (!daySearch) {
				keepThisAd = true;
			} else {
				Elements dateElements = rootElement.getElementsByAttributeValue("class", "date");
				if (dateElements.size()>0){
					Element dateElement = dateElements.get(0).child(0);
					if (dateElement.text().contains("Hier")){				
						keepThisAd = true;
					}
				}	
			}
			
			// On ne mets pas les google ads
//			Boolean isGoogleAd = true;
//			Elements googleAds = tr.getElementsContainingText("google");
//			if (googleAds.size() == 0){
//				googleAds = tr.getElementsContainingText("Google");
//				if (googleAds.size() == 0){
//					isGoogleAd = false;
//				}
//			}
//			
//			if (keepThisAd && !isGoogleAd){
//				keepThisAd = false;
//			}
			
			if (keepThisAd){
				result.add(rootElement);
			}
		}
		
		return result;
	}
	
	
	public Boolean populateTitle(Element element, ClassifiedBean classifiedBean){

		Boolean isTitle = false; // Ce TD contient-il le titre de l'annonce + prix?

		// Titre 
		Elements aList = element.getElementsByAttributeValue("class", "title");
		for (Iterator<Element> aIterator = aList.iterator(); aIterator.hasNext();) {
			Element a = aIterator.next();

			if (a.hasText()){
				// C'est le titre de l'annonce
				classifiedBean.setTitle(a.text());
				isTitle = true;				
			}
		}
							
		// Lien 
		classifiedBean.setLink(element.attr("href"));
		
		return isTitle;
	}
}
